﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApp.BaseDataSvc;
using YY.Common;
using System.Collections;
using System.Web.Script.Serialization;

namespace WebApp
{
    public partial class EditWorkFlowFrm : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(EditWorkFlowFrm), this);
            var orderId = Request.Params["OrderId"];
            if (string.IsNullOrEmpty(orderId))
            {
                Response.Write("非法进入");
                Response.End();
            }
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<WFResult> GetWF(int orderId)
        {
            AjaxResult<WFResult> result = new AjaxResult<WFResult>();
            result.Data = null;
            result.Msg = null;
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                WFResult wfr = new WFResult();
                wfr.WF = this.BaseDataProxy.GetCurrentWorkflow(order);
                wfr.BtnStatus = getBtnStatus(order, wfr.WF);
                result.Data = wfr;
            }
            catch (Exception ex)
            {
                result.Data = null;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> ConfirmWF(int orderId, int op, string comment)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Handle, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_User user = Session["User"] as TB_User;
                if (op != user.id)
                {
                    result.Msg = "不是环节处理人没有处理权限！";
                    return result;
                }

                MsgBoolean mb = this.BaseDataProxy.HandleWorkflow2(orderId, 0, null, null, comment, user.id, user.role);
                result.Data = mb.Ok;
                if (!mb.Ok)
                    result.Msg = mb.Msg;
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        [AjaxPro.AjaxMethod]
        public AjaxResult<bool> LZWF(int orderId, int nextOp, int opType, string comment)
        {
            AjaxResult<bool> result = new AjaxResult<bool>();
            result.Data = false;
            result.Msg = null;
            try
            {
                if (!this.IsPermitOP(EnumCommon.PermissionOpType.Handle, this.GetType().Name))
                {
                    result.Msg = "没有操作权限！";
                    return result;
                }

                TB_User user = Session["User"] as TB_User;
                Type t = typeof(EnumCommon.OrderStatus);
                var order = this.BaseDataProxy.GetOrder(orderId);
                var wf = this.BaseDataProxy.GetCurrentWorkflow(order);
                int current = (int)Enum.Parse(t, wf.flowStatus);
                int next = current + opType;

                if (opType == 2)
                    next = current;

                MsgBoolean mb = this.BaseDataProxy.HandleWorkflow2(orderId, opType, nextOp, Enum.GetName(t, next), comment, user.id, user.role);
                result.Data = mb.Ok;
                if (!mb.Ok)
                    result.Msg = mb.Msg;
            }
            catch (Exception ex)
            {
                result.Data = false;
                result.Msg = ex.Message;
            }
            return result;
        }

        string[] getBtnStatus(TB_Order order,TB_Workflow wf)
        {
            string[] rtn = new string[] { "disable", "disable", "disable" };//代表前端按顺序排的按钮
            try
            {
                TB_User user = (Session["User"] as TB_User);
                bool pass = false;
                if (order.isSubmit)
                {
                    if (user.role == (int)EnumCommon.SysRoleType.财务员)
                        pass = true;
                }
                else
                {
                    if (user.role == (int)EnumCommon.SysRoleType.客服员)
                        pass = true;
                }
                rtn[0] = (!wf.isHandle & user.id == wf.@operator) ? "enable" : "disable";
                rtn[1] = ((order.status != EnumCommon.OrderStatus.已收款.ToString() | order.status != EnumCommon.OrderStatus.取消.ToString()) & pass) ? "enable" : "disable";
                rtn[2] = (wf.flowStatus.Equals("操作完成") | wf.flowStatus.Equals("接单确认")) ? "enable" : "disable";
                //rtn[3] = ((order.status != EnumCommon.OrderStatus.已收款.ToString() | order.status != EnumCommon.OrderStatus.取消.ToString()) & pass) ? "enable" : "disable";
            }
            catch { }
            return rtn;
        }

        [AjaxPro.AjaxMethod]
        public string GetUserByWF(int cbIndex, int orderId)
        {
            Hashtable ht = new Hashtable();
            try
            {
                var order = this.BaseDataProxy.GetOrder(orderId);
                TB_Workflow wf = this.BaseDataProxy.GetCurrentWorkflow(order);
                Type t = typeof(EnumCommon.OrderStatus);
                IEnumerable<TB_User> users = this.BaseDataProxy.FindUsers(null, null);
                TB_User user = Session["User"] as TB_User;
                IEnumerable<TB_User> us = null;
                int workflowIntStatus = (int)Enum.Parse(t, wf.flowStatus);

                if (cbIndex == 0)//下一环节
                {
                    if (workflowIntStatus < 3 && !(workflowIntStatus == 2 && wf.isHandle))//客服处理
                    {
                        us = users.Where(p => p.id == user.id);
                    }
                    else//财务处理
                    {
                        if (user.role == (int)EnumCommon.SysRoleType.财务员)
                            us = users.Where(p => p.id == user.id);
                        else
                            us = users.Where(p => p.role == (int)EnumCommon.SysRoleType.财务员);
                    }
                }
                else if (cbIndex == 1)
                {
                    if (workflowIntStatus == 3)//财务退回客服
                        us = users.Where(p => p.id == order.agent);
                    else//客服退回自己或财务退回自己
                        us = users.Where(p => p.id == user.id);
                }
                else
                {
                    us = users.Where(p => p.role == (int)EnumCommon.SysRoleType.客服员);
                }
                ht.Add("rows", us);
                ht.Add("total", us.Count());
            }
            catch { }
            return new JavaScriptSerializer().Serialize(ht); ;
        }

        public class WFResult
        {
            public TB_Workflow WF { get; set; }
            public string[] BtnStatus { get; set; }
        }
    }
}
